package cn.geminis.lowcode.compiler;

import cn.geminis.lowcode.compiler.util.JdkCompilerClass;
import cn.geminis.lowcode.utils.Utils;
import lombok.Getter;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.util.Elements;

/**
 * @author puddi
 */
@Getter
public class Context {

    public static final Context INSTANCE = new Context();

    private ProcessingEnvironment processingEnv;

    private Object treeMaker;

    private Elements elementUtils;

    private Object classWriter;

    private Object symtab;
    private Object enter;
    private Object gen;
    private Object javaCompiler;
    private Object lambdaToMethod;

    private Context() {
    }

    public void init(ProcessingEnvironment processingEnv) {
        this.processingEnv = processingEnv;
        LcModule.addOpens();
        var context = Utils.callMethod(this.processingEnv, "getContext");
        this.treeMaker = Utils.callMethod(JdkCompilerClass.TREE_MAKER, "instance", context);
        this.classWriter = Utils.callMethod(JdkCompilerClass.CLASS_WRITER, "instance", context);
        this.symtab = Utils.callMethod(JdkCompilerClass.SYMTAB, "instance", context);
        this.enter = Utils.callMethod(JdkCompilerClass.ENTER, "instance", context);
        this.gen = Utils.callMethod(JdkCompilerClass.GEN, "instance", context);
        this.javaCompiler = Utils.callMethod(JdkCompilerClass.JAVA_COMPILER, "instance", context);
        this.lambdaToMethod = Utils.callMethod(JdkCompilerClass.LAMBDA_TO_METHOD, "instance", context);
        this.elementUtils = processingEnv.getElementUtils();
    }
}
